package cn.ybk.pet.service.impl;

import cn.ybk.basic.util.*;
import cn.ybk.order.domain.OrderPetAcquisition;
import cn.ybk.order.mapper.OrderPetAcquisitionMapper;
import cn.ybk.org.domain.Shop;
import cn.ybk.org.mapper.ShopMapper;
import cn.ybk.pet.domain.Pet;
import cn.ybk.pet.domain.SearchMasterMsg;
import cn.ybk.pet.domain.SearchMasterMsgAuditLog;
import cn.ybk.pet.dto.AcceptDto;
import cn.ybk.pet.dto.PublishDto;
import cn.ybk.pet.mapper.PetMapper;
import cn.ybk.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.ybk.pet.mapper.SearchMasterMsgMapper;
import cn.ybk.pet.query.SearchMasterMsgQuery;
import cn.ybk.pet.service.ISearchMasterMsgService;
import cn.ybk.basic.service.impl.BaseServiceImpl;
import cn.ybk.user.domain.Logininfo;
import cn.ybk.user.domain.User;
import cn.ybk.user.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author È¥ÄãÂëµÄ£¡
 * @since 2023-03-16
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderMapper;

    // 宠物寻主
    @Override
    public void publish(PublishDto dto) {
        // 校验数据
        SearchMasterMsg searchMasterMsg = new SearchMasterMsg();
        // 将数据添加到数据库
        Logininfo logininfo = LoginContext.getLogininfo();
        User user =  userMapper.findByLogininfo(logininfo.getId());
        searchMasterMsg.setUserId(user.getId());
        BeanUtils.copyProperties(dto,searchMasterMsg);
        searchMasterMsg.setPetType(dto.getPetType());
        searchMasterMsgMapper.add(searchMasterMsg);
        // 百度ai审核名字 审核失败添加日志
        if (!BaiduAuditUtils.TextCensor(dto.getName())){
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setAuditId(-1L);
            log.setAuditTime(new Date());
            log.setNote("宠物名称违规：" + dto.getName() + "!");
            log.setState(0);
            log.setMsgId(searchMasterMsg.getId());
            System.out.println(searchMasterMsg.getId());
            searchMasterMsgAuditLogMapper.add(log);
            return;
        }
        // 随机分配50公里以内的店铺
        Point point = DistanceUtil.getPoint(dto.getAddress());
        List<Shop> shops = shopMapper.findAll();
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        if (nearestShop != null) {
            // 有店铺
            searchMasterMsg.setShopId(nearestShop.getId());
            searchMasterMsg.setState(1);
            searchMasterMsgMapper.update(searchMasterMsg);
        }
        // 没有店铺
    }

    // 接单 成功
    @Override
    public void accept(AcceptDto dto) {
        // 参数校验
        // 查询msg
        SearchMasterMsg msg = searchMasterMsgMapper.findById(dto.getMsgId());
        // 添加pet列表
        Pet pet = new Pet();
        pet.setState(0);
        pet.setCreatetime(new Date());
        pet.setName(msg.getName());
        pet.setCostprice(BigDecimal.valueOf(msg.getPrice()));
        pet.setSearchMasterMsgId(msg.getId());
        pet.setShopId(msg.getShopId());
        pet.setTypeId(msg.getPetType());
        petMapper.add(pet);
        //1.生成收购单
        OrderPetAcquisition order = new OrderPetAcquisition();
        order.setAddress(msg.getAddress());
        order.setEmpId(dto.getHandler());
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(msg.getUserId()));
        order.setDigest("你有新的美团外卖");
        order.setLastConfirmTime(new Date(System.currentTimeMillis()+ 2*24*60*60*1000));
        // 0 是待支付 1 是支付
        order.setState(0);
        order.setPayType(1);
        order.setPetId(pet.getId());
        order.setUserId(msg.getUserId());
        order.setShopId(msg.getShopId());
        order.setEmpId(dto.getHandler());
        order.setMsgId(dto.getMsgId());
        order.setPrice(BigDecimal.valueOf(msg.getPrice()));
        orderMapper.add(order);

        //3.修改状态为已接单，state = 3
        msg.setState(3);
        searchMasterMsgMapper.update(msg);
        System.out.println("完成");

    }
    // 拒接接单
    @Override
    public void reject(Long id) {
        System.out.println(id);
        searchMasterMsgMapper.updateByShopId(id);
        System.out.println("完成");
    }

}
